package com.jf.identity.entity;

import com.jf.identity.util.ExamIdAndExamSessionIdUtil;
import com.jf.identity.util.Params;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author FCG
 */
@Component
public class ExamBaseParamValidator implements Serializable {
    private static final long serialVersionUID = -7698458453804377474L;
    public final static String ORGCODE = "orgCode";
    public final static String EXAM_ID = "examId";
    public final static String EXAM_SESSION_NUM = "examSessionNum";
    public final static String LOGIC_EXAM_ROOM_NUM = "logicExamRoomNum";
    public final static String DEVICE_ID = "deviceId";
    @Resource
    private ExamIdAndExamSessionIdUtil examIdAndExamSessionIdUtil;
    public void validateExamPlan(String examPlan){
        int flag = 0;
        List<ExamInfo> examInfoList = examIdAndExamSessionIdUtil.getExamIdCatch();
        for (ExamInfo e : examInfoList) {
            if (e.getExamId().equals(examPlan)) {
                flag = 1;
            }
        }
        if(flag != 1){
            throw new IllegalArgumentException("请输入正确的考试计划ID");
        }
    }
    public void validate(ExamBaseParam examBaseParam, String... fields) {
        if (examBaseParam == null || fields == null || fields.length == 0) {
            return;
        }
        for (String field : fields) {
            switch (field) {
                case ExamBaseParam.ORGCODE:
                    Params.isBlank(examBaseParam.getOrgCode(), "机构代码");
                    if (examBaseParam.getOrgCode().length() > 50) {
                        throw new IllegalArgumentException("机构代码小于50个字符");
                    }
                    break;
                case ExamBaseParam.EXAM_ID:
                    Params.isBlank(examBaseParam.getExamId(), "考试计划");
                    if (examBaseParam.getExamId().length() > 10) {
                        throw new IllegalArgumentException("考试计划小于10个字符");
                    }
                    //todo：增加对examId的校验
                    int flag = 0;
                    List<ExamInfo> examInfoList = examIdAndExamSessionIdUtil.getExamIdCatch();
                    for (ExamInfo e : examInfoList) {
                        if (e.getExamId().equals(examBaseParam.getExamId())) {
                            flag = 1;
                        }
                    }
                    if(flag == 1){
                        break;
                    }else {
                        throw new IllegalArgumentException("请输入正确的考试计划ID");
                    }

                case ExamBaseParam.EXAM_SESSION_NUM:
                    Params.isBlank(examBaseParam.getExamSessionNum(), "考试场次");
                    if (examBaseParam.getExamSessionNum().length() > 2) {
                        throw new IllegalArgumentException("考试场次小于2个字符");
                    }
                    List<ExamSessionInfo> examSessionInfoList = examIdAndExamSessionIdUtil.getExamSessionIdCatch();
                    flag = 0;
                    for (ExamSessionInfo e : examSessionInfoList) {
                        if (e.getExamId().equals(examBaseParam.getExamId()) &&
                                e.getExamSessionNum().equals(examBaseParam.getExamSessionNum())) {
                            flag = 1;
                        }
                    }
                    if(flag == 1){
                        break;
                    }else {
                        throw new IllegalArgumentException("此考试计划下没有对应的场次");
                    }
                case ExamBaseParam.LOGIC_EXAM_ROOM_NUM:
                    Params.isBlank(examBaseParam.getLogicExamRoomNum(), "逻辑考场号");
                    if (examBaseParam.getLogicExamRoomNum().length() > 50) {
                        throw new IllegalArgumentException("逻辑考场号小于50个字符");
                    }
                    break;
                case ExamBaseParam.DEVICE_ID:
                    Params.isBlank(examBaseParam.getDeviceId(), "设备编号");
                    if (examBaseParam.getDeviceId().length() > 20) {
                        throw new IllegalArgumentException("设备id小于20个字符");
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
